package WWW::NikePlus::Goal;

use strict;

our $VERSION = '0.01';

my %goal_type_map = (
  runFarther => 'distanceInKM',
  runFaster => 'numberOfRuns',
  runCalories => 'calories',
  runMoreOften => 'numberOfRuns',
);

sub new {
  my $class = shift(@_);
  my $self  = {@_};

  foreach my $required (qw{ id content }) {
    die(__PACKAGE__ . "::new requires parameter: [$required]\n")
      unless (exists($self->{$required}));
  }
  
  $self->{'complete'} = ($self->{'content'}{'complete'} eq 'true');
  $self->{'completedTime'} = $self->{'content'}{'completedTime'};
  $self->{'startTime'} = $self->{'content'}{'startTime'};
  $self->{'endTime'} = $self->{'content'}{'endTime'};
  $self->{'type'} = $self->{'content'}{'definition'}{'type'};
  $self->{'progress'} = $self->{'content'}{'definition'}{'totalProgress'};

  my $field = $goal_type_map{$self->{'type'}};
  $self->{'goal'} = $self->{'content'}{'definition'}{$field};

  my %sched;
  foreach my $day (grep{ /^run.*day$/o } keys %{$self->{'content'}{'schedule'}}){
    my $val = $day;
    $val =~ s/^run//o;
    $sched{$val} = $self->{'content'}{'schedule'}{$day}; 
  }
  $self->{'schedule'} = \%sched;

  delete($self->{'content'});  
  $self = bless($self, $class);

  return $self;
}

sub getID{ return $_[0]->{'id'}; }
sub isComplete{ return $_[0]->{'complete'}; }
sub getCompletedTime{ return $_[0]->{'completedTime'}; }
sub getType{ return $_[0]->{'type'}; }
sub getStartTime{ return $_[0]->{'startTime'}; }
sub getEndTime{ return $_[0]->{'endTime'}; }
sub getProgress{ return $_[0]->{'progress'}; }
sub getGoal{ return $_[0]->{'goal'}; }
sub getSchedule{ return $_[0]->{'schedule'}; }
